Test Failed
Pull Request — master (#2)
by Luís
03:32 queued 01:49
created

Request.send   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
c 1
b 0
f 1
nc 2
nop 5
dl 0
loc 32
rs 8.8571
1
import App from "app";
2
3
const sa = App.libs.Superagent;
4
const EM = App.ServicesContainer.getNewInstance("EventManager");
5
6
/**
7
 * @callback requestCallback
8
 * @param {mixed}   error
9
 * @param {Object}  response
10
 * @param {Object}  reqquest
11
 */
12
13
/**
14
 * @callback successfulRequestCallback
15
 * @param {mixed}   error
16
 * @param {Object}  response
17
 * @param {Object}  reqquest
18
 */
19
20
/**
21
 * Utility class to make XHR requests
22
 * 
23
 * @class Request
24
 */
25
class Request {
26
    constructor() {
27
        this.EM = EM;
28
    }
29
30
    /**
31
     * Register a new event listener to be called when a new request is started
32
     * @param  {Function} parameters: req
0 ignored issues
show
Documentation introduced by
The parameter parameters: does not exist. Did you maybe forget to remove this comment?
Loading history...
33
     * @return {Request}
34
     */
35
    onStart(cb) {
36
        "use strict";
37
38
        this.EM.subscribe("start", cb);
39
        return this;
40
    }
41
42
    /**
43
     * Register a new event listener to be called when an request is completed
44
     * @param  {requestCallback} cb
45
     * @return {Request}
46
     */
47
    onStop(cb) {
48
        "use strict";
49
50
        this.EM.subscribe("stop", cb);
51
        return this;
52
    }
53
54
    /**
55
     * Register a new event listener to be called when an request fails
56
     * @param  {requestCallback} cb
57
     * @return {Request}
58
     */
59
    onError(cb) {
60
        "use strict";
61
62
        this.EM.subscribe("error", cb);
63
        return this;
64
    }
65
66
    /**
67
     * Register a new event listener to be called when an request succeeds
68
     * @param  {successfulRequestCallback} cb
69
     * @return {Request}
70
     */
71
    onSuccess(cb) {
72
        "use strict";
73
74
        this.EM.subscribe("success", cb);
75
        return this;
76
    }
77
78
    /**
79
     * make a xhr
80
     * @param  {String} method    get|post|put|delete|del
81
     * @param  {String} url
82
     * @param  {Object} data
83
     * @param  {successfulRequestCallback} onSuccess function to be called if the request succeeds
84
     * @param  {requestCallback} onError   function to be called if the request fails
85
     * @return {null}
86
     */
87
    send(method, url, data, onSuccess, onError) {
88
        "use strict";
89
90
        const req = sa[method](url);
91
92
        if (method === "get") {
93
            req.query(data);
94
        } else {
95
            req.send(data);
96
        }
97
98
        this.EM.notify("start", req);
99
100
        req.end((err, res) => {
101
            this.EM.notify("stop", err, res, req);
102
103
            if (err) {
104
                this.EM.notify("error", err, res, req);
105
106
                if (onError) {
107
                    onError(err, res, req);
108
                }
109
            } else {
110
                this.EM.notify("success", res, req);
111
112
                if (onSuccess) {
113
                    onSuccess(res, req);
114
                }
115
            }
116
        });
117
    }
118
119
    /**
120
     * make a xhr with more options
121
     * @param  {Object} config
122
     * @param  {String} config.url
0 ignored issues
show
Documentation Bug introduced by
The parameter config.url does not exist. Did you maybe mean config instead?
Loading history...
123
     * @param  {String} config.method get|post|put|del|delete
0 ignored issues
show
Documentation Bug introduced by
The parameter config.method does not exist. Did you maybe mean config instead?
Loading history...
124
     * @param  {Function} config.onStart function to call before send the resquest
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onStart does not exist. Did you maybe mean config instead?
Loading history...
125
     * @param  {requestCallback} config.onStop
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onStop does not exist. Did you maybe mean config instead?
Loading history...
126
     * @param  {successfulRequestCallback} config.onSuccess
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onSuccess does not exist. Did you maybe mean config instead?
Loading history...
127
     * @param  {requestCallback} config.onError
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onError does not exist. Did you maybe mean config instead?
Loading history...
128
     * @param  {Object} config.headers
0 ignored issues
show
Documentation Bug introduced by
The parameter config.headers does not exist. Did you maybe mean config instead?
Loading history...
129
     * @param  {Object} config.data
0 ignored issues
show
Documentation Bug introduced by
The parameter config.data does not exist. Did you maybe mean config instead?
Loading history...
130
     */
131
    make(config) {
132
        "use strict";
133
134
        let defaultOptions = {
135
            url: "",
136
            method: "get",
137
            onStart: null,
138
            onStop: null,
139
            onSuccess: null,
140
            onError: null,
141
            headers: {},
142
            data: {}
143
        };
144
145
        let options = Object.assign({}, defaultOptions, config);
146
147
        if (options.method === "delete") {
148
            options.method = "del";
149
        }
150
151
        let req = sa[options.method](options.url).send(options.data);
152
153
        this.EM.notify("start", req);
154
        if (typeof options.onStart === "function") {
155
            options.onStart(req);
156
        }
157
158
        // headers
159
        Object.keys(options.headers).map((header) => req.set(header, options.headers[header]));
160
161
        req.end((err, res) => {
162
            this.EM.notify("stop", err, res, req);
163
            if (typeof options.onStop === "function") {
164
                options.onStop(err, res, req);
165
            }
166
167
            if (err) {
168
                this.EM.notify("error", err, res, req);
169
                if (typeof options.onError === "function") {
170
                    options.onError(err, res, req);
171
                }
172
            } else {
173
                this.EM.notify("success", res, req);
174
175
                if (typeof options.onSuccess === "function") {
176
                    options.onSuccess(res, req);
177
                }
178
            }
179
        });
180
    }
181
}
182
183
export default Request;
184